package edu.fjut.se1603.yuan.dao.impl;

import edu.fjut.se1603.yuan.dao.AccountDao;
import edu.fjut.se1603.yuan.dao.BaseDao;
import edu.fjut.se1603.yuan.entity.Account;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AccountDaoImpl extends BaseDao implements AccountDao {
    @Override
    public int addData(Account account) {
        int flag = 0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("insert into account (PET_ID, OWNER_ID, STORE_ID, PRICE, DEAL_TIME,DEAL_TYPE) values (?,?,?,?,CURRENT_DATE(),?)");
            preparedStatement.setInt(1, account.getPetId());
            preparedStatement.setInt(2, account.getOwnerId());
            preparedStatement.setInt(3, account.getStoreId());
            preparedStatement.setDouble(4, account.getPrice());
            preparedStatement.setInt(5, account.getDealType());
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public void getStoreAccount(int storeId) {
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(
                    "select account.ID ID, pet.NAME  PET_NAME, " +
                            "pet_owner.NAME OWNER_NAME, " +
                            "pet_store.NAME STORE_NAME, DEAL_TYPE, DEAL_TIME " +
                            "from account, pet, pet_store, pet_owner " +
                            "where account.PET_ID = pet.ID and " +
                            "account.STORE_ID = pet_store.ID and " +
                            "account.OWNER_ID = pet_owner.ID and " +
                            "account.ID in " +
                            "(select ID " +
                            "from account " +
                            "where STORE_ID = ?)" +
                            "order by ID");
            preparedStatement.setInt(1, storeId);
            ResultSet set = preparedStatement.executeQuery();
            while (set.next()) {
                int id=set.getInt("ID");
                String petName=set.getString("PET_NAME");
                String ownerName= set.getString("OWNER_NAME");
                String storeName=set.getString("STORE_NAME");
                String dealType=set.getInt("DEAL_TYPE")==1?"买":"卖";
                Date dealTime=set.getDate("DEAL_TIME");
                System.out.println(id+" "+petName+" "+ownerName+" "+storeName+" "+dealType+" "+dealTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


}
